<template>
  <a-modal
    :title="title"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="regionOperationVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="行政区划代码"
                       v-bind="formItemLayout">
            <a-input v-decorator="['regionCode',{rules: [{ required: true, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>

          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="行政区划名称"
                       v-bind="formItemLayout">
            <a-input v-decorator="['regionName',{rules: [{ required: true, message: '必填' },
            { pattern: '^[\u4e00-\u9fa5a-zA-Z][\u4e00-\u9fa5a-zA-Z\\d]+$', message: '行政区划名称首字符必须是汉字或字母'}]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="行政级别"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              @select="editParentCode"
              :disabled="this.isUpd"
              v-decorator="['regionLevel',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option key="01">省级</a-select-option>
              <a-select-option key="02">地市级</a-select-option>
              <a-select-option key="03">区县级</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="上级行政区划代码"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              :disabled="this.parentCodeEdit"
              @focus="selectParentCodeData"
              @search="selectParentCodeData"
              @select="assignParentName"
              v-decorator="['parentCode']">
              <a-select-option v-for="(d,index) in parentCodeData" :key="index">
                {{ d.id }}-{{ d.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="上级行政区划名称"
                       v-bind="formItemLayout">
            <a-input v-decorator="['parentName']"
                     :disabled="true"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="对应银联地区代码"
                       v-bind="formItemLayout">
            <a-input v-decorator="['areaCode',{rules: [{ required: true, message: '必填' },
            { pattern: '^[a-zA-Z0-9]{4}$', message: '输入长度为4个的字母或数字'}]}]" :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="对应支付宝旧版地区代码"
                       v-bind="formItemLayout">
            <a-input
              v-decorator="['alipayAdcode',{rules:[{ pattern: '^[a-zA-Z0-9]{4}$', message: '输入长度为4个的字母或数字'}]}]"
              :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button" v-show="this.title !== '查看行政区划代码'">
      <a-button @click="onClose" type="primary">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'RegionOperation',
    props: {
      regionOperationVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        title: '新增行政区划代码',
        isStop: false,
        isCancel: false,
        parentCodeEdit: false,
        isUpd: false,
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        parentCodeData: [],
        info: {},

      }
    },
    computed: {
      show: {
        get: function () {
          return this.regionOperationVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      editParentCode(val) {
        if (val == '01') {
          this.parentCodeEdit = true
        } else {
          this.parentCodeEdit = false
        }
      },

      selectParentCodeData() {
        this.parentCodeData = []
        let regionLevel = this.form.getFieldsValue(['regionLevel'])

        if (regionLevel.regionLevel) {
          let param = {}
          if (regionLevel.regionLevel == '02') {
            param.l = '01'
            this.$get('admin/merParam/regionInfo/select2', param).then((r) => {
              let result = r.data
              result.forEach(r => {
                this.parentCodeData.push(r);
              })
            })
          } else if (regionLevel.regionLevel == '03') {
            param.l = '02'
            this.$get('admin/merParam/regionInfo/select4', param).then((r) => {
              let result = r.data
              result.forEach(r => {
                this.parentCodeData.push(r);
              })
            })
          }

        } else {
          this.$message.warning("请先选择行政级别!")
          return;
        }

      },

      assignParentName(val) {
        this.form.setFieldsValue({
          parentCode: this.parentCodeData[val].id,
          parentName: this.parentCodeData[val].name
        })
      },

      reset() {
        this.loading = false
        this.isCancel = false
        this.isUpd = false
        this.isStop = false
        this.parentCodeEdit = false
        this.title = '新增行政区划代码'
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.setInfoFields()
            this.loading = true
            this.info.instId = this.instId

            if (this.title == '新增行政区划代码') {
              this.info.optFlag = 'add'
            } else if (this.title == '修改行政区划代码') {
              this.info.optFlag = 'upd'
            } else if (this.title == '注销行政区划代码') {
              this.info.optFlag = 'cancel'
            }

            if (this.title == '新增行政区划代码') {
              this.$post('/admin/merParam/regionInfo', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            } else if (this.title == '修改行政区划代码') {
              this.$put('/admin/merParam/regionInfo/update', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            } else {
              this.$put('/admin/merParam/regionInfo/cancel', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            }

          }
        })
      },

      setInfoFields() {
        let values = this.form.getFieldsValue(['id', 'regionCode', 'regionName', 'regionLevel', 'parentCode', 'parentName', 'areaCode', 'alipayAdcode'])
        if (typeof values !== 'undefined') {
          Object.keys(values).forEach(_key => {
            this.info[_key] = values[_key]
          })
        }
      },

      setRegionFormValues({...info}) {
        let fields = ['id', 'regionCode', 'regionName', 'regionLevel', 'parentCode', 'parentName', 'areaCode', 'alipayAdcode']
        if (info.operation == 'cancel') {
          this.isCancel = true
          this.isUpd = true
          this.title = '注销行政区划代码'
          this.parentCodeEdit = true
        } else if (info.operation == 'view') {
          this.isUpd = true
          this.parentCodeEdit = true
          this.title = '查看行政区划代码'
        } else {
          this.title = '修改行政区划代码'
        }
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

    }

  }
</script>




